package org.kgduyjj.toolkit.enumtype;

import java.util.regex.Pattern;
/**
 * 基于正则表达式的校验器枚举对象
 * 
 * @author Kgduyjj
 * @since 1.2.0
 *
 */
public enum Validator {

    /** 英文字母 、数字和下划线 */
    GENERAL(Pattern.compile("^\\w+$")),
    
    /** 数字 */
    NUMBERS(Pattern.compile("\\d+")),
    
    /** 字母 */
    WORD(Pattern.compile("[a-zA-Z]+")),
    
    /** 单个中文汉字 */
    CHINESE(Pattern.compile("[\\u4E00-\\u9FFF]")),
    
    /** 中文汉字 */
    CHINESES(Pattern.compile("[\\\\u4E00-\\\\u9FFF]+")),
    
    /** IP v4 */
    IPV4(Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b")),
    
    /** IP v6 */
    IPV6(Pattern.compile("(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))")),
    
    /** 货币 */
    MONEY(Pattern.compile("^(\\d+(?:\\.\\d+)?)$")),
    
    /** 邮件，符合RFC 5322规范，正则来自：http://emailregex.com/ */
    // EMAIL(Pattern.compile("(\\w|.)+@\\w+(\\.\\w+){1,2}")),
    EMAIL(Pattern.compile("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)])", Pattern.CASE_INSENSITIVE)),
    
    /** 移动电话 */
    MOBILE(Pattern.compile("(?:0|86|\\+86)?1[3456789]\\d{9}")),
    
    /** 18位身份证号码 */
    CITIZEN_ID(Pattern.compile("[1-9]\\d{5}[1-2]\\d{3}((0\\d)|(1[0-2]))(([012]\\d)|3[0-1])\\d{3}(\\d|X|x)")),
    
    /** 邮编 */
    ZIP_CODE(Pattern.compile("[1-9]\\d{5}(?!\\d)")),
    
    /** 生日 */
    BIRTHDAY(Pattern.compile("^(\\d{2,4})([/\\-.年]?)(\\d{1,2})([/\\-.月]?)(\\d{1,2})日?$")),
    
    /** URL */
    URL(Pattern.compile("[a-zA-z]+://[^\\s]*")),
    
    /** Http URL */
    URL_HTTP(Pattern.compile("(https://|http://)?([\\w-]+\\.)+[\\w-]+(:\\d+)*(/[\\w- ./?%&=]*)?")),
    
    /** 中文字、英文字母、数字和下划线 */
    GENERAL_WITH_CHINESE(Pattern.compile("^[\u4E00-\u9FFF\\w]+$")),
    
    /** UUID */
    UUID(Pattern.compile("^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$")),
    
    /** 不带横线的UUID */
    UUID_SIMPLE(Pattern.compile("^[0-9a-z]{32}$")),
    
    /** 中国车牌号码 */
    PLATE_NUMBER(Pattern.compile("^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z][A-Z][A-Z0-9]{4}[A-Z0-9挂学警港澳]$")),
    
    /** MAC地址正则 */
    MAC_ADDRESS(Pattern.compile("((?:[A-F0-9]{1,2}[:-]){5}[A-F0-9]{1,2})|(?:0x)(\\d{12})(?:.+ETHER)", Pattern.CASE_INSENSITIVE)),

    /** 16进制字符串 */
    HEX(Pattern.compile("^[a-f0-9]+$", Pattern.CASE_INSENSITIVE)),
    
    /** 时间正则 */
    TIME(Pattern.compile("\\d{1,2}:\\d{1,2}(:\\d{1,2})?")),
    // END
    ;
    /** 校验公式 */
    protected Pattern pattern;

    Validator(Pattern pattern) {
        this.pattern = pattern;
    }
    
    /**
     * 给定内容是否匹配正则
     * 
     * @param content 内容
     * @return 正则为null或者""则不检查，返回true，内容为null返回false
     */
    public boolean isMatch(CharSequence content) {
        if (content == null || pattern == null) {
            // 提供null的字符串为不匹配
            return false;
        }
        return pattern.matcher(content).matches();
    }

}
